const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: 0,
    navBar: {},
    iscart: false,
    cart: [], //数据
    selectedCart: [],
    isSelectAll: false,
    count: 1, //商品数量默认是1
    total: 0, //总金额
    goodsCount: 0 //数量
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this
    this.setData({
      navBar: app.globalData.navBar
    })
    // 设置屏幕可用高度
    this.setData({
      height: wx.getSystemInfoSync().windowHeight - that.data.navBar.navigationBarAndStatusBarHeight
    })
  },
  // 计算总数和总金额
  countTotal(arr) {
    let selectedArr = arr.filter(item => item.isSelect)
    let total = 0,
      goodsCount = 0
    if (selectedArr.length) {
      for (let i = 0; i < selectedArr.length; i++) {
        total += Number(selectedArr[i].price) * Number(selectedArr[i].count);
        goodsCount += Number(selectedArr[i].count);
      }
    }
    this.setData({
      selectedCart: selectedArr,
      total,
      goodsCount
    })
  },


  // 计算单个商品数量
  changeCount(e) {
    let cart = this.data.cart
    for (let i = 0; i < cart.length; i++) {
      if (this.data.cart[i].id == e.currentTarget.id) {
        // 判断是count+还是count-
        if (e.currentTarget.dataset.count == 'del') {
          // 判断count数量，大于1则减1
          if (this.data.cart[i].count > 1) {
            this.data.cart[i].count = this.data.cart[i].count - 1
          }
        } else {
          this.data.cart[i].count = this.data.cart[i].count + 1
        }
        this.setData({
          cart
        })
        wx.setStorageSync('cart', cart)
        this.countTotal(cart)
        return
      }
    }
  },

  // 全选
  selectAll() {
    let {
      cart,
      isSelectAll
    } = this.data
    // 判断购物车列表是否为空
    if (cart.length) {
      // 全选取反，然后给商品列表设置相同select
      isSelectAll = !isSelectAll
      cart.map(item => item.isSelect = isSelectAll)
      this.setData({
        isSelectAll,
        cart
      })
      wx.setStorageSync('cart', cart)
      // 计算总数和总价格
      this.countTotal(cart)
    }
  },
  // 删除商品
  delGoods(e) {
    let cart = this.data.cart
    for (let i = 0; i < cart.length; i++) {
      if (cart[i].id == e.currentTarget.id) {
        cart = cart.filter(item=>item.id != cart[i].id)
        this.setData({cart})
        this.countTotal(cart)
        wx.setStorageSync('cart', cart)
      }
    }
  },

  // countDelTotal(arr){
  //   console.log(123)
  //   let selectedArr = arr.filter(item => item.isSelect)
  //   console.log(selectedArr)
  //   for (let i in selectedArr) {
  //     this.data.total += Number(selectedArr[i].price) * Number(selectedArr[i].count);
  //     this.data.goodsCount += Number(selectedArr[i].count);
  //   }
  //   this.setData({
  //     selectedCart:selectedArr,
  //     total: this.data.total,
  //     goodsCount: this.data.goodsCount
  //   })
  // },

  changeSelect(e) {
    let {
      cart,
      isSelectAll
    } = this.data
    let that = this
    // 修改选中状态
    // 判断数组是否为空
    if (cart.length) {
      for (let i = 0; i < cart.length; i++) {
        if (this.data.cart[i].id == e.currentTarget.id) {
          cart[i].isSelect = !cart[i].isSelect
          this.setData({
            cart
          })
          wx.setStorageSync('cart', cart)
          this.countTotal(cart)
          // 判断购物车列表和已选商品列表是否相等，若相等，全选勾上
          if (that.data.cart.length === that.data.selectedCart.length && that.data.cart.length != 0) {
            isSelectAll = true
          } else {
            isSelectAll = false
          }
          this.setData({
            isSelectAll
          })
          return
        }
      }
    }
  },




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this;
    // 获取产品展示页保存的缓存数据（购物车的缓存数组，没有数据，则赋予一个空数组）     
    let arr = wx.getStorageSync('cart') || [];
    // 有数据的话,过滤出选中商品，遍历数据，计算总金额 和 总数量
    if (arr.length > 0) {
      that.countTotal(arr)
      // 更新数据
      this.setData({
        iscart: true,
        cart: arr,
      });
      let isSelectAll
      if (that.data.cart.length === that.data.selectedCart.length && that.data.cart.length != 0) {
        isSelectAll = true
      } else {
        isSelectAll = false
      }
      this.setData({
        isSelectAll
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    this.setData({
      iscart: false,
      cart: [], //数据
      total: 0, //总金额
      goodsCount: 0 //数量
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})